﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022   保留所有权利。
 * CLR版本：4.0.30319.42000
 * 机器名称：DESKTOP-UL84BU5
 * 公司名称：
 * 命名空间：KingerRobot.WanliWMS.SpiService.Services.Warehouse
 * 唯一标识：12ebf505-a2fb-4260-8841-12f0df48f18f
 * 文件名：WarehouseManageImpl
 * 当前用户域：DESKTOP-UL84BU5
 * 
 * 创建者：zzl
 * 电子邮箱：1003590782@qq.com
 * 创建时间：2022/7/4 13:19:54
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System.Data;

namespace KingerRobot.MexicoSailunWMS.SpiService;
//WMS.Warehouse
public class BWarehouseManageImpl : IWarehouseManageService
{

    private readonly static IRcsEquipWarehouse _RcsEquipWarehouse = new BRcsEquipWarehouse();
    private readonly static IWcsEquipLocHold _WcsEquipLocHold = new BWcsEquipLocHold();
    private readonly static IWcsAsrsCrnInput _WcsAsrsCrnInput = new BWcsAsrsCrnInput();
    private readonly static IWcsAsrsCrnOutPut _WcsAsrsCrnOutPut = new BWcsAsrsCrnOutPut();
    private readonly static IRcsEquipCrn _RcsEquipCrn = new BRcsEquipCrn();
    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();



    public List<WarehouseNameBean> GetAllWarehouseNameBeans()
    {
        List<PORcsEquipWarehouse> rcsEquipWarehouseList = _RcsEquipWarehouse.GetPORcsEquipWarehouses();
        List<WarehouseNameBean> warehouseNameList = new List<WarehouseNameBean>();

        rcsEquipWarehouseList.ForEach(item =>
        {
            WarehouseNameBean warehouseName = new()
            {
                WarehouseId = item.Id ?? 0,
                WarehouseName = item.WarehouseName,
            };
            warehouseNameList.Add(warehouseName);
        });
        return warehouseNameList;
    }

    public List<InWarehouseCountBean> GetInWarehouseCountBeans(WarehouseCountParam warehouseCountParam)
    {
        var result = _WcsAsrsCrnInput.GetAsrsCrnInput();


        if (!string.IsNullOrWhiteSpace(warehouseCountParam.CrnCode.Trim()) && Convert.ToInt64(warehouseCountParam.CrnCode.Trim()) > 0)//指定堆垛机编号
        {
            result = result.Where(t => t.CrnCode == Convert.ToInt64(warehouseCountParam.CrnCode.Trim())).ToList();
        }

        if (!string.IsNullOrWhiteSpace(warehouseCountParam.CrnName.Trim()))
        {//指定堆垛机名称
            result = result.Where(t => t.CrnName == warehouseCountParam.CrnName.Trim()).ToList();
        }

        return result.Select(t => new InWarehouseCountBean
        {
            Id = t.Id,
            AreaName = t.AreaName,
            CrnName = t.CrnName,
            Number = t.Number,
            InputLoc1 = t.InputLoc1,
            InputLoc2 = t.InputLoc2,
            Reason = t.Reason,
            EquipEnable = t.EquipEnable
        }).OrderBy(t => t.Id).ToList();
    }

    public List<WcsAsrsCrnCountBean> GetInWarehouseCountBeansV2()
    {
        var result = _WcsAsrsCrnInput.GetAsrsCrnInputV2();
        return result;
    }
    

    public List<OutWarehouseCountBean> GetOutWarehouseCountBeans(WarehouseCountParam warehouseCountParam)
    {
        var result = _WcsAsrsCrnOutPut.GetAsrsCrnOutPut();


        if (!string.IsNullOrWhiteSpace(warehouseCountParam.CrnCode) && Convert.ToInt64(warehouseCountParam.CrnCode.Trim()) > 0)//指定堆垛机编号
        {
            result = result.Where(t => t.CrnCode == Convert.ToInt64(warehouseCountParam.CrnCode.Trim())).ToList();
        }

        if (!string.IsNullOrWhiteSpace(warehouseCountParam.CrnName.Trim()))
        {//指定堆垛机名称
            result = result.Where(t => t.CrnName == warehouseCountParam.CrnName.Trim()).ToList();
        }

        return result.Select(t => new OutWarehouseCountBean
        {
            Id = t.Id,
            AreaName = t.AreaName,
            CrnName = t.CrnName,
            Number = t.Number,
            OutputLoc1 = t.OutputLoc1,
            OutputLoc2 = t.OutputLoc2,
            Reason = t.Reason,
            EquipEnable = t.EquipEnable
        }).OrderBy(t => t.Id).ToList();
    }

    public List<WarehouseBean> GetWarehouseBeans()
    {
        var result = _RcsEquipWarehouse.GetPORcsEquipWarehouses();

        List<WarehouseBean> warehouseBeans = new();


        //可用货位数量(所有库区)
        var rcslocs = _RcsLocation.GetAllLocations();
        //不可用货位数量(所有库区)
        var rcsDisableLocs = _RcsLocation.GetAllDisableLocations();

        result.ForEach(item =>
        {
            var locs = _WcsEquipLocHold.GetEquipLocHoldsByWarehouse(item.Id.GetValueOrDefault());
            //所有库位数量
            var locCount = locs.Count();


            //关联出当前库区的可用货位
            var useLocs = locs.Join(rcslocs, f => f.LocId, s => s.Id, (f, s) => new
            {
                locs = f,
                rcslocs = s,
            }).ToList();
            //所有可用货位数量
            var useLocCount = useLocs.Count();

            //所有可用空库位数量
            var useLocEmptyCount = useLocs.Where(t => t.rcslocs.LocHasSku == 0).Count();


            //关联出当前库区的禁用货位
            var disableLocCount = locs.Join(rcsDisableLocs, f => f.LocId, s => s.Id, (f, s) => new
            {
                locs = f,
                rcslocs = s,
            }).ToList().Count();

            var crns = _RcsEquipCrn.GetEquipCrnsByWarehouse(item.Id.GetValueOrDefault());
            //所有堆垛机数量
            var crnCount = crns.Count();
            ////所有可用堆垛机数量
            //var usecrnsCount = locs.Where(t => t.UseFlag == 1).Count();

            // 可用性
            var useFlag = item.UseFlag == 1 ? true : false;

            warehouseBeans.Add(new()
            {
                Id = item.Id.GetValueOrDefault(),
                WarehouseName = item.WarehouseName,
                LocCount = locCount,
                UseFlag = useFlag,
                UseLocCount = useLocCount,
                UseLocEmptyCount = useLocEmptyCount,
                DisableLocCount = disableLocCount,
                CrnCount = crnCount,
            });
        });

        return warehouseBeans;
    }

    [OperationLog("修改入库限制数量")]
    public int UpdateInWarehouseCount(UpdateWarehouseCountParam warehouseCountParam, long userId, string userName, string clientHost)
    {
        if (warehouseCountParam is null)
        {
            return -1;
        }
        if (warehouseCountParam.Number < 0)
        {
            return -2;
        }

        var id = warehouseCountParam.Id;
        var number = warehouseCountParam.Number;
        var reason = warehouseCountParam.Remark;

        var result = _WcsAsrsCrnInput.UpdateAsrsCrnInputCount(id, number, reason);

        return result;

    }

    [OperationLog("修改出库限制数量")]
    public int UpdateOutWarehouseCount(UpdateWarehouseCountParam warehouseCountParam, long userId, string userName, string clientHost)
    {
        if (warehouseCountParam is null)
        {
            return -1;
        }
        if (warehouseCountParam.Number < 0)
        {
            return -2;
        }

        var id = warehouseCountParam.Id;
        var number = warehouseCountParam.Number;
        var reason = warehouseCountParam.Remark;

        var result = _WcsAsrsCrnOutPut.UpdateAsrsCrnOutPutCount(id, number, reason);

        return result;
    }

    [OperationLog("仓库启用禁用")]
    public int UpdateWarehouseUserFlag(long id, long userId, string userName, string clientHost)
    {
        var result = _RcsEquipWarehouse.UpdateWarehousesUseFlag(id);
        return result;
    }



}
